var keywords = require('../models/keyword'),
    apiHandler = require('../services/api-handler');

user = require('../models/user');

exports.getWordCloud = function (req, res) {
    keywords.getWordCloud(req.body, apiHandler.handler.check(res, 'NO_KEYWORDS', function (keywords) {
        var data = {
            keywordList: keywords,
            keywordCount: keywords.length
        };
        apiHandler.setSuccessResponse(data, res);
    }));
};

exports.getUserKeywords = function (req, res) {
    keywords.getUserKeywords(req.user._id, apiHandler.handler.check(res, 'NO_KEYWORDS', function (keywords) {
        var data = {
            keywordList: keywords,
            keywordCount: keywords.length
        };
        apiHandler.setSuccessResponse(data, res);
    }));
};

exports.findKeywordsByWords = function (req, res) {
    keywords.findKeywordsByWords(req.body.words, apiHandler.handler.check(res, 'NO_KEYWORDS', function (keywords) {
        var data = {
            keywordList: keywords,
            keywordCount: keywords.length
        };
        apiHandler.setSuccessResponse(data, res);
    }));
};




exports.findKeywordsByPercent = function (req, res) {
    keywords.getKeyWordCountPercent(req.body.words, apiHandler.handler.check(res, 'NO_IDEAS', function (keywordData){

        var result = {
            count: 0,
            percent: 0,
        };

        if(keywordData.length > 0){
            // 如果存在关键字，则查询全部用户数目
            user.getTotalUserCount(apiHandler.handler.check(res, 'NO_IDEAS', function (userData){
                let totalUserNum = userData[0].total
                let keyWordUserNum = keywordData[0].total

                result.count = keyWordUserNum
                result.percent = (keyWordUserNum / totalUserNum).toFixed(3)
                apiHandler.setSuccessResponse(result, res);

            }));
        }else{

            apiHandler.setSuccessResponse(result, res);
        }


    }));
};